﻿Imports System.Data.SqlClient
Public Class Frm_ijra2at
    Sub remplirHeuSal()
        If dv.List_ijraa.Count > 0 Then
            Dim ijr As ijraa = dv.List_ijraa.Item(dv.List_ijraa.Count - 1)
            txt_heur.Text = ijr.Heure
            txt_salle.Text = ijr.Salle
        End If
    End Sub
  

    Private Sub Btn_rech_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_rech.Click
        If Comb_marji3ona.SelectedIndex = -1 Then
            MsgBox("اختر الملف أولا", MsgBoxStyle.Critical, "خطأ في الإضافة")
        Else
            Me.Enabled = False
            Frm_Rech_ijra2at.Show()

        End If
    End Sub

    Private Sub Frm_ijra2at_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Partager.close()
        Try
            Dim i As Integer = 0
            Partager.nonInitial = New ArrayList
            Partager.nonInitial.Add("Comb_marji3ona")
            Partager.remplirDossier(Comb_marji3ona)

            If Partager.idDossier <> 0 Then
                For Each item In Comb_marji3ona.Items
                    Partager.deClt = DirectCast(item, DictionaryEntry)
                    If CInt(Partager.deClt.Key) = Partager.idDossier Then
                        Comb_marji3ona.SelectedIndex = i
                    End If
                    i += 1
                Next
                Partager.idDossier = 0
            End If
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
        lblJour.Text = Partager.getDayName(Date_ijra2.Value)
    End Sub
    Public dv As Dossier
    Private Sub Btn_ajout_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_ajout.Click
        If (List_tous.SelectedIndices.Count > 0) Then
            MsgBox("عفوا لا يمكنك الإضافة، لإضافة إجراء جديد عليك الضغط على الزر جديد ثم إدخال البيانات", MsgBoxStyle.Critical, "خطأ في الإضافة")
        ElseIf Comb_marji3ona.SelectedIndex = -1 Then
            MsgBox("اختر الملف أولا", MsgBoxStyle.Critical, "خطأ في الإضافة")
        Else
            Try
                If MsgBox("هل تريد فعلا إضافة هذا الإجراء ؟", MsgBoxStyle.YesNo Or MsgBoxStyle.Information, "إضافة إجراء") = MsgBoxResult.Yes Then
                    Partager.close()

                    Dim cli As New ijraa(1, Comb_ijra2.Text, txt_salle.Text, Date_ijra2.Value, txt_mokalaf_ijra2.Text, txt_man_ata.Text, lblJour.Text, txt_heur.Text, txt_cause.Text, txt_molahadat.Text)
                    dv.Ajouter_ijraa(cli)

                    List_tous.Items.Clear()
                    For Each khib As ijraa In dv.List_ijraa
                        List_tous.Items.Add(New ListViewItem(New String() {khib.Ijraa, khib.Date_ijraa, khib.Jour, khib.Heure, khib.Salle, khib.Responsable_ijraa, khib.Cause, khib.Serveur_ijraa}))
                    Next
                    MsgBox("تمت إضافة الإجراء بنجاح", MsgBoxStyle.Information, "نجاح")
                    Partager.vider(Me, Partager.nonInitial)
                    Date_ijra2.Value = Now
                    lblJour.Text = Partager.getDayName(Date_ijra2.Value)
                    'Comb_ijra2.SelectedIndex = -1
                    remplirHeuSal()
                End If
            Catch sqlEx As SqlException
                If sqlEx.Message.Contains("IX_Ijraa") Then
                    MsgBox("عفوا، هذا التاريخ لإجراء آخر", MsgBoxStyle.Critical, "خطأ")
                Else
                    MsgBox(sqlEx.Message)
                End If
            Catch ex As Exception
                MsgBox(ex.Message)
            End Try
        End If
    End Sub

    Private Sub Btn_modif_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_modif.Click

        If (List_tous.SelectedIndices.Count <= 0) Then
            MsgBox("عفوا لا يمكنك التعديل، اختر اجراء من القائمة", MsgBoxStyle.Critical, "خطأ في التعديل")
        ElseIf Comb_marji3ona.SelectedIndex = -1 Then
            MsgBox("اختر الملف أولا", MsgBoxStyle.Critical, "خطأ في التعديل")
        Else
            Try
                If MsgBox("هل تريد فعلا تعديل هذا الإجراء ؟", MsgBoxStyle.YesNo Or MsgBoxStyle.Information, "تعديل إجراء") = MsgBoxResult.Yes Then
                    Partager.close()
                    Dim cli As New ijraa(1, Comb_ijra2.Text, txt_salle.Text, Date_ijra2.Value, txt_mokalaf_ijra2.Text, txt_man_ata.Text, lblJour.Text, txt_heur.Text, txt_cause.Text, txt_molahadat.Text)
                    dv.modifier_ijraa(cli, avo.Id)
                    List_tous.Items.Clear()
                    For Each khib As ijraa In dv.List_ijraa
                        List_tous.Items.Add(New ListViewItem(New String() {khib.Ijraa, khib.Date_ijraa, khib.Jour, khib.Heure, khib.Salle, khib.Responsable_ijraa, khib.Cause, khib.Serveur_ijraa}))
                    Next
                    MsgBox("تمت تعديل الإجراء بنجاح", MsgBoxStyle.Information, "نجاح")
                    Partager.vider(Me, Partager.nonInitial)
                    'Comb_ijra2.SelectedIndex = -1
                    Date_ijra2.Value = Now
                    lblJour.Text = Partager.getDayName(Date_ijra2.Value)
                    remplirHeuSal()
                End If
            Catch sqlEx As SqlException
                If sqlEx.Message.Contains("IX_Ijraa") Then
                    MsgBox("عفوا، هذا التاريخ لإجراء آخر", MsgBoxStyle.Critical, "خطأ")
                Else
                    MsgBox(sqlEx.Message)
                End If
            Catch ex As Exception

                MsgBox(ex.Message)

            End Try
        End If
    End Sub
    Public avo As ijraa
    Private Sub List_tous_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles List_tous.SelectedIndexChanged
        Try
            For Each i As Integer In List_tous.SelectedIndices
                avo = dv.List_ijraa(i)

                Comb_ijra2.Text = avo.Ijraa
                txt_salle.Text = avo.Salle
                Date_ijra2.Value = avo.Date_ijraa
                txt_mokalaf_ijra2.Text = avo.Responsable_ijraa
                txt_man_ata.Text = avo.Serveur_ijraa
                lblJour.Text = avo.Jour
                txt_heur.Text = avo.Heure
                txt_cause.Text = avo.Cause
                txt_molahadat.Text = avo.Remarque

            Next
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
        If List_tous.SelectedIndices.Count < 1 Then
            Partager.vider(Me, Partager.nonInitial)
            remplirHeuSal()
        End If
    End Sub


    Private Sub Btn_supp_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_supp.Click

        If List_tous.SelectedIndices.Count > 0 Then
            If MsgBox("هل تريد فعلا حذف هذا الاجراء ؟", MsgBoxStyle.YesNo Or MsgBoxStyle.Information, "حذف اجراء") = MsgBoxResult.Yes Then
                Partager.close()
                Try
                    Dim clt As ijraa = dv.List_ijraa(List_tous.SelectedIndices.Item(0))
                    dv.suprimmer_ijraa(avo.Id)

                    List_tous.Items.Clear()
                    For Each khib As ijraa In dv.List_ijraa
                        List_tous.Items.Add(New ListViewItem(New String() {khib.Ijraa, khib.Date_ijraa, khib.Jour, khib.Heure, khib.Salle, khib.Responsable_ijraa, khib.Cause, khib.Serveur_ijraa}))
                    Next
                    Partager.vider(Me, Partager.nonInitial)
                    Date_ijra2.Value = Now
                    lblJour.Text = Partager.getDayName(Date_ijra2.Value)
                    remplirHeuSal()
                    MsgBox("عملية الحذف تمت بنجاح", MsgBoxStyle.Information, "نجاح")
                Catch ex As Exception
                    MsgBox(ex.Message)
                End Try
            End If
        Else
            MsgBox("اختر اجراء من القائمة", MsgBoxStyle.Information, "خطأ")
        End If
    End Sub

    Private Sub Btn_nouv_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_nouv.Click
        If Comb_marji3ona.Text <> "" Then
            Partager.vider(Me, Partager.nonInitial)
            Date_ijra2.Value = Now
            lblJour.Text = Partager.getDayName(Date_ijra2.Value)
            remplirHeuSal()
        End If

        
    End Sub

    Private Sub Date_ijra2_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Date_ijra2.ValueChanged
        lblJour.Text = Partager.getDayName(Date_ijra2.Value)
    End Sub

   

    Private Sub Comb_marji3ona_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Comb_marji3ona.SelectedIndexChanged
        Partager.vider(Me, Partager.nonInitial)
        Try
            Partager.deClt = DirectCast(Comb_marji3ona.SelectedItem, DictionaryEntry)
            For Each d As Dossier In Partager.moi.List_info_fichier
                If d.id = CInt(Partager.deClt.Key) Then
                    dv = d
                    dv.remplir_List_ijraa()
                End If
            Next
            List_tous.Items.Clear()
            remplirHeuSal()
            For Each khib As ijraa In dv.List_ijraa
                List_tous.Items.Add(New ListViewItem(New String() {khib.Ijraa, khib.Date_ijraa, khib.Jour, khib.Heure, khib.Salle, khib.Responsable_ijraa, khib.Cause, khib.Serveur_ijraa}))
            Next
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

    Private Sub Btn_Rech_Doss_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_Rech_Doss.Click

        Me.Enabled = False
        Partager.frmafect = Me
        Frm_Recherche.Show()
        Partager.type = Me.Name

    End Sub
End Class